package com.demo.util;

import com.demo.service.StagesDetailService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class SpringUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    public void setApplicationContext(ApplicationContext ac) throws BeansException {
        applicationContext = ac;
    }

    private SpringUtil() {
        log.info("SpringUtil created");
    }

    public static Object getObject(String id) {
        return applicationContext.getBean(id);
    }

    public static <T> T getObject(Class<T> tClass) {
        return applicationContext.getBean(tClass);
    }

    /*public static Object getBean(Class<StagesDetailService> tClass) {
        return applicationContext.getBean(tClass);
    }*/

    public <T> T getBean(Class<T> tClass) {
        return applicationContext.getBean(tClass);
    }

    public void destroy() throws Exception {
        log.info("SpringUtil destroy");
        applicationContext = null;
    }
}
